#!/usr/bin/python3
# -*- coding: utf-8 -*-

import re
from typing import List

from playwright.sync_api import Page, expect

from utils.log import log


class Assert:
    def __init__(self, page: Page):
        self.page = page
        self.timeout = 10000  # 默认的断言超时时间

    def to_be_checked(self, locator, value):
        """验证复选框是否被选中"""
        expect(locator).to_be_checked(value, timeout=self.timeout)

    def to_be_disabled(self, locator):
        """验证元素是否被禁用"""
        expect(locator).to_be_disabled(timeout=self.timeout)

    def to_be_editable(self, locator):
        """验证元素是否可编辑"""
        expect(locator).to_be_editable(timeout=self.timeout)

    def to_be_empty(self, locator):
        """验证容器是否为空"""
        expect(locator).to_be_empty(timeout=self.timeout)

    def to_be_enabled(self, locator):
        """验证元素是否启用"""
        expect(locator).to_be_enabled(timeout=self.timeout)

    def to_be_focused(self, locator):
        """验证元素是否获得焦点"""
        expect(locator).to_be_focused(timeout=self.timeout)

    def to_be_hidden(self, locator):
        """验证元素是否隐藏"""
        expect(locator).to_be_hidden(timeout=self.timeout)

    def to_be_visible(self, locator):
        """验证元素是否可见"""
        expect(locator).to_be_visible(timeout=self.timeout)

    def to_contain_text(self, locator, text):
        """验证元素是否包含指定的文本"""
        expect(locator).to_contain_text(text, timeout=self.timeout)

    def to_have_attribute(self, locator, attribute, value):
        """验证元素是否具有指定的属性"""
        expect(locator).to_have_attribute(attribute, value, timeout=self.timeout)

    def to_have_class(self, locator, class_name):
        """验证元素是否具有指定的类名"""
        expect(locator).to_have_class(class_name, timeout=self.timeout)

    def to_have_count(self, locator, count):
        """验证元素个数是否与期望值相等"""
        expect(locator).to_have_count(count, timeout=self.timeout)

    def to_have_css(self, locator, css_property, value):
        """验证元素是否具有指定的CSS属性"""
        expect(locator).to_have_css(css_property, value, timeout=self.timeout)

    def to_have_id(self, locator, id):
        """验证元素是否具有指定的ID"""
        expect(locator).to_have_id(id, timeout=self.timeout)

    def to_have_js_property(self, locator, js_propert):
        """验证元素是否具有指定的JavaScript属性"""
        expect(locator).to_have_js_property(js_propert, timeout=self.timeout)

    def to_have_text(self, locator, text):
        """验证元素是否具有指定的文本内容"""
        expect(locator).to_have_text(text, timeout=self.timeout)

    def to_have_value(self, locator, value):
        """验证表单元素是否具有指定的值"""
        log.info(f"locator:{locator} 包含的值为: {value}")
        expect(locator).to_have_value(value, timeout=self.timeout)

    def to_have_values(self, locators: List, values: List):
        """验证表单元素是否具有指定的值集合"""
        expect(locators).to_have_value(values, timeout=self.timeout)

    def to_have_title(self, title):
        """验证页面标题是否与期望值相等"""
        expect(self.page).to_have_title(re.compile(title), timeout=self.timeout)

    def to_have_url(self, text):
        """验证页面URL是否与期望值相等"""
        log.info(f"page:{self.page} 包含的url为: {text}")
        expect(self.page).to_have_url(re.compile(text), timeout=self.timeout)

    def to_be_ok(self, api_response):
        """验证API响应状态码是否为200 ok"""
        expect(api_response).to_be_ok()

    def not_to_be_ok(self, api_response):
        """验证API响应状态码是否不为200 ok"""
        expect(api_response).not_to_be_ok()
